home *** CD-ROM | disk | FTP | other *** search
/ Aminet 6 / Aminet 6 - June 1995.iso / Aminet / gfx / 3d / irit50src.lha / irit5 / prsr_lib / coerce.c < prev    next >
Encoding:
C/C++ Source or Header  |  1994-12-06  |  9.2 KB  |  271 lines

  1. /******************************************************************************
  2. * coerce.c - coerce an object into a different type.                  *
  3. *******************************************************************************
  4. * Written by Gershon Elber, January 1993.                      *
  5. ******************************************************************************/
  6.  
  7. #include <stdio.h>
  8. #include "irit_sm.h"
  9. #include "iritprsr.h"
  10. #include "allocate.h"
  11.  
  12. /*****************************************************************************
  13. * DESCRIPTION:                                                               M
  14. *  Given a set of points, returns the list's common denominator that spans   M
  15. * the space of all the points, taking into account type Type.             M
  16. *                                                                            *
  17. * PARAMETERS:                                                                M
  18. *   PtObjList:  List of points.                                              M
  19. *   Type:       Point type that we must span its space as well.              M
  20. *                                                                            *
  21. * RETURN VALUE:                                                              M
  22. *   CagdPointType:   Point type that spans the space of point type Type as   M
  23. *                    well as all points in PtObjList.                        M
  24. *                                                                            *
  25. * KEYWORDS:                                                                  M
  26. *   IritPrsrCoerceCommonSpace, coercion                                      M
  27. *****************************************************************************/
  28. CagdPointType IritPrsrCoerceCommonSpace(IPObjectStruct *PtObjList, int Type)
  29. {
  30.     int i,
  31.     Dim = CAGD_NUM_OF_PT_COORD(Type),
  32.     Proj = CAGD_IS_RATIONAL_PT(Type);
  33.     IPObjectStruct *PtObj;
  34.  
  35.     if (!IP_IS_OLST_OBJ(PtObjList)) {
  36.     IritPrsrFatalError("Coerce: Not a list object!");
  37.     return CAGD_PT_NONE;
  38.     }
  39.  
  40.     for (i = 0; (PtObj = ListObjectGet(PtObjList, i++)) != NULL; ) {
  41.     if (IP_IS_CTLPT_OBJ(PtObj)) {
  42.         Dim = MAX(Dim, CAGD_NUM_OF_PT_COORD(PtObj -> U.CtlPt.PtType));
  43.         Proj |= CAGD_IS_RATIONAL_PT(PtObj -> U.CtlPt.PtType);
  44.     }
  45.     else if (IP_IS_POINT_OBJ(PtObj) || IP_IS_VEC_OBJ(PtObj)) {
  46.         Dim = MAX(Dim, 3);
  47.     }
  48.     else {
  49.         IritPrsrFatalError("Coerce: Not a point object in list!");
  50.         return CAGD_PT_NONE;
  51.     }
  52.     }
  53.  
  54.     return CAGD_MAKE_PT_TYPE(Proj, Dim) ;
  55. }
  56.  
  57. /*****************************************************************************
  58. * DESCRIPTION:                                                               M
  59. * Coerces a list of objects to Type.                         M
  60. *                                                                            *
  61. * PARAMETERS:                                                                M
  62. *   PtObjList:   Coerce points/vectors/control points in this list to Type.  M
  63. *   Type:        Minimum space type to coerce to in PtObjList.               M
  64. *                                                                            *
  65. * RETURN VALUE:                                                              M
  66. *   CagdPointType:   The type coercion actualy took place with in PtObjList. M
  67. *                                                                            *
  68. * KEYWORDS:                                                                  M
  69. *   IritPrsrCoercePtsListTo, coercion                                        M
  70. *****************************************************************************/
  71. CagdPointType IritPrsrCoercePtsListTo(IPObjectStruct *PtObjList, int Type)
  72. {
  73.     int i;
  74.     IPObjectStruct *TmpObj, *PtObj;
  75.     CagdPointType
  76.     PtType = IritPrsrCoerceCommonSpace(PtObjList, Type);
  77.  
  78.     if (PtType != CAGD_PT_NONE) {
  79.     for (i = 0; (PtObj = ListObjectGet(PtObjList, i++)) != NULL; ) {
  80.         if (IP_IS_CTLPT_OBJ(PtObj)) {
  81.         TmpObj = IritPrsrCoerceObjectTo(PtObj, PtType);
  82.         PtObj -> U.CtlPt = TmpObj -> U.CtlPt;
  83.         IPFreeObject(TmpObj);
  84.         }
  85.         else if (IP_IS_POINT_OBJ(PtObj) || IP_IS_VEC_OBJ(PtObj)) {
  86.         TmpObj = IritPrsrCoerceObjectTo(PtObj, PtType);
  87.         ReallocNewTypeObject(PtObj, IP_OBJ_CTLPT);
  88.         PtObj -> U.CtlPt = TmpObj -> U.CtlPt;
  89.         IPFreeObject(TmpObj);
  90.         }
  91.     }
  92.     }
  93.  
  94.     return PtType;
  95. }
  96.  
  97. /*****************************************************************************
  98. * DESCRIPTION:                                                               M
  99. * Coerces an object to a new object.                         M
  100. *   Points, vectors, control points and planes can always be coerced between M
  101. * themselves using this routine by specifying the new object type desired    M
  102. * such as IP_OBJ_PLANE or control point type like CAGD_PT_E4_TYPE.          M
  103. *   Control points of curves and surfaces may be coerced to a new type by    M
  104. * prescribing the needed point type as NewType, such as CAGD_PT_P2_TYPE.     M
  105. *                                                                            *
  106. * PARAMETERS:                                                                M
  107. *   PObj:      Object to coerce.                                             M
  108. *   NewType:   New type which can be object type like IP_OBJ_VECTOR or point M
  109. *              type like E2.                             M
  110. *                                                                            *
  111. * RETURN VALUE:                                                              M
  112. *   IPObjectStruct *:   Newly coerced object.                                M
  113. *                                                                            *
  114. * KEYWORDS:                                                                  M
  115. *   IritPrsrCoerceObjectTo, coercion                                         M
  116. *****************************************************************************/
  117. IPObjectStruct *IritPrsrCoerceObjectTo(IPObjectStruct *PObj, int NewType)
  118. {
  119.     int i;
  120.     RealType Pt[CAGD_MAX_PT_SIZE], *R;
  121.     CagdSrfStruct *NewSrf;
  122.     IPObjectStruct
  123.     *NewObj = NULL;
  124.  
  125.     switch (PObj -> ObjType) {
  126.     case IP_OBJ_POINT:
  127.     case IP_OBJ_VECTOR:
  128.     case IP_OBJ_PLANE:
  129.     case IP_OBJ_CTLPT:
  130.         Pt[0] = 1.0;
  131.         for (i = 1; i < CAGD_MAX_PT_SIZE; i++)
  132.             Pt[i] = 0.0;
  133.         switch (PObj -> ObjType) {
  134.         case IP_OBJ_POINT:
  135.             PT_COPY(&Pt[1], PObj -> U.Pt);
  136.             break;
  137.         case IP_OBJ_VECTOR:
  138.             PT_COPY(&Pt[1], PObj -> U.Vec);
  139.             break;
  140.         case IP_OBJ_PLANE:
  141.             PLANE_COPY(&Pt[1], PObj -> U.Plane);
  142.             break;
  143.         case IP_OBJ_CTLPT:
  144.             R = PObj -> U.CtlPt.Coords;
  145.             CagdCoercePointTo(&Pt[1], CAGD_PT_E5_TYPE,
  146.                       &R, -1, PObj -> U.CtlPt.PtType);
  147.             break;
  148.         default:
  149.             break;
  150.         }
  151.  
  152.         switch (NewType) {
  153.         case IP_OBJ_POINT:
  154.             NewObj = GenPTObject(&Pt[1], &Pt[2], &Pt[3]);
  155.             break;
  156.         case IP_OBJ_VECTOR:
  157.             NewObj = GenVECObject(&Pt[1], &Pt[2], &Pt[3]);
  158.             break;
  159.         case IP_OBJ_PLANE:
  160.             NewObj = GenPLANEObject(&Pt[1], &Pt[2], &Pt[3], &Pt[4]);
  161.             break;
  162.         case IP_OBJ_CTLPT:
  163.             NewObj = GenCTLPTObject(CAGD_PT_E3_TYPE, NULL, Pt);
  164.             break;
  165.         case CAGD_PT_P1_TYPE:
  166.         case CAGD_PT_P2_TYPE:
  167.         case CAGD_PT_P3_TYPE:
  168.         case CAGD_PT_P4_TYPE:
  169.         case CAGD_PT_P5_TYPE:
  170.             if (PObj -> ObjType == IP_OBJ_CTLPT)
  171.                 CagdCoercePointTo(Pt, CAGD_PT_P5_TYPE,
  172.                       &R, -1, PObj -> U.CtlPt.PtType);
  173.         case CAGD_PT_E1_TYPE:
  174.         case CAGD_PT_E2_TYPE:
  175.         case CAGD_PT_E3_TYPE:
  176.         case CAGD_PT_E4_TYPE:
  177.         case CAGD_PT_E5_TYPE:
  178.             NewObj = GenCTLPTObject(NewType, NULL, Pt);
  179.             break;            
  180.         default:
  181.             break;            
  182.         }
  183.         break;
  184.     case IP_OBJ_CURVE:
  185.         switch (NewType) {
  186.         case CAGD_PT_E1_TYPE:
  187.         case CAGD_PT_E2_TYPE:
  188.         case CAGD_PT_E3_TYPE:
  189.         case CAGD_PT_E4_TYPE:
  190.         case CAGD_PT_E5_TYPE:
  191.         case CAGD_PT_P1_TYPE:
  192.         case CAGD_PT_P2_TYPE:
  193.         case CAGD_PT_P3_TYPE:
  194.         case CAGD_PT_P4_TYPE:
  195.         case CAGD_PT_P5_TYPE:
  196.             NewObj = GenCRVObject(CagdCoerceCrvTo(PObj -> U.Crvs,
  197.                               NewType));
  198.             break;
  199.         default:
  200.             break;
  201.         }
  202.         break;
  203.     case IP_OBJ_SURFACE:
  204.         switch (NewType) {
  205.         case CAGD_PT_E1_TYPE:
  206.         case CAGD_PT_E2_TYPE:
  207.         case CAGD_PT_E3_TYPE:
  208.         case CAGD_PT_E4_TYPE:
  209.         case CAGD_PT_E5_TYPE:
  210.         case CAGD_PT_P1_TYPE:
  211.         case CAGD_PT_P2_TYPE:
  212.         case CAGD_PT_P3_TYPE:
  213.         case CAGD_PT_P4_TYPE:
  214.         case CAGD_PT_P5_TYPE:
  215.             NewObj = GenSRFObject(CagdCoerceSrfTo(PObj -> U.Srfs,
  216.                               NewType));
  217.             break;
  218.         default:
  219.             break;
  220.         }
  221.         break;
  222.     case IP_OBJ_TRIVAR:
  223.         switch (NewType) {
  224.         case CAGD_PT_E1_TYPE:
  225.         case CAGD_PT_E2_TYPE:
  226.         case CAGD_PT_E3_TYPE:
  227.         case CAGD_PT_E4_TYPE:
  228.         case CAGD_PT_E5_TYPE:
  229.         case CAGD_PT_P1_TYPE:
  230.         case CAGD_PT_P2_TYPE:
  231.         case CAGD_PT_P3_TYPE:
  232.         case CAGD_PT_P4_TYPE:
  233.         case CAGD_PT_P5_TYPE:
  234.             NewObj = GenTRIVARObject(TrivTVCopyList(PObj -> U.Trivars));
  235.             NewSrf = CagdCoerceSrfTo(NewObj -> U.TrimSrfs -> Srf,
  236.                          NewType);
  237.             CagdSrfFreeList(NewObj -> U.TrimSrfs -> Srf);
  238.             NewObj -> U.TrimSrfs -> Srf = NewSrf;
  239.             break;
  240.         default:
  241.             break;
  242.         }
  243.         break;
  244.     case IP_OBJ_TRIMSRF:
  245.         switch (NewType) {
  246.         case CAGD_PT_E1_TYPE:
  247.         case CAGD_PT_E2_TYPE:
  248.         case CAGD_PT_E3_TYPE:
  249.         case CAGD_PT_E4_TYPE:
  250.         case CAGD_PT_E5_TYPE:
  251.         case CAGD_PT_P1_TYPE:
  252.         case CAGD_PT_P2_TYPE:
  253.         case CAGD_PT_P3_TYPE:
  254.         case CAGD_PT_P4_TYPE:
  255.         case CAGD_PT_P5_TYPE:
  256.             NewObj = GenTRIMSRFObject(TrimSrfNew(
  257.             CagdCoerceSrfTo(PObj -> U.TrimSrfs -> Srf, NewType),
  258.             TrimCrvCopyList(PObj -> U.TrimSrfs -> TrimCrvList),
  259.             TRUE));
  260.             break;
  261.         default:
  262.             break;
  263.         }
  264.         break;
  265.     default:
  266.         break;
  267.     }
  268.  
  269.     return NewObj;
  270. }
  271.